﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Neudesic.Pulse.Events;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;

namespace Neudesic.Pulse.ViewModels
{
    /// <summary>
    /// View model for the shell.
    /// </summary>
    [Export]
    public class ShellViewModel : ViewModel
    {

        /// <summary>
        /// Gets the last exceptions.
        /// </summary>
        /// <value>The last exception.</value>
        public ObservableCollection<Exception> Exceptions { get; private set; }

        /// <summary>
        /// Gets or sets the clear command.
        /// </summary>
        /// <value>The clear command.</value>
        public DelegateCommand ClearCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ShellViewModel" /> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        [ImportingConstructor]
        public ShellViewModel(IEventAggregator eventAggregator) : base(eventAggregator)
        {

            Exceptions = new ObservableCollection<Exception>();

            ClearCommand = new DelegateCommand(
                () =>
                {
                    Exceptions.Clear();
                }
                );

            // subscribe to exception notification so we can notify the shell
            EventAggregator.GetEvent<ApplicationExceptionEvent>().Subscribe(
                (ex) =>
                {
                    Exceptions.Insert(0, ex); // insert at beginning
                }, ThreadOption.UIThread // Need to publish on UI thread so updates are reflected.
                );

        }


    }
}
